home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / image / directco.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  6.0 KB  |  206 lines

  1. /*
  2.  * @(#)DirectColorModel.java    1.9 95/09/08 Jim Graham
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.awt.image;
  21.  
  22. import java.awt.AWTException;
  23.  
  24. /**
  25.  * A ColorModel class that specifies a translation from pixel values
  26.  * to alpha, red, green, and blue color components for pixels which
  27.  * have the color components embedded directly in the bits of the
  28.  * pixel itself.  This color model is similar to an X11 TrueColor
  29.  * visual.
  30.  *
  31.  * @see ColorModel
  32.  *
  33.  * @version    1.9 09/08/95
  34.  * @author     Jim Graham
  35.  */
  36. public class DirectColorModel extends ColorModel {
  37.     private int red_mask;
  38.     private int green_mask;
  39.     private int blue_mask;
  40.     private int alpha_mask;
  41.     private int red_offset;
  42.     private int green_offset;
  43.     private int blue_offset;
  44.     private int alpha_offset;
  45.     private int red_bits;
  46.     private int green_bits;
  47.     private int blue_bits;
  48.     private int alpha_bits;
  49.  
  50.     /**
  51.      * Construct a DirectColorModel from the given masks specifying
  52.      * which bits in the pixel contain the red, green and blue color
  53.      * components.  Pixels described by this color model will all
  54.      * have alpha components of 255 (fully opaque).  All of the bits
  55.      * in each mask must be contiguous and fit in the specified number
  56.      * of least significant bits of the integer.
  57.      */
  58.     public DirectColorModel(int bits,
  59.                 int rmask, int gmask, int bmask) {
  60.     this(bits, rmask, gmask, bmask, 0);
  61.     }
  62.  
  63.     /**
  64.      * Construct a DirectColorModel from the given masks specifying
  65.      * which bits in the pixel contain the alhpa, red, green and blue
  66.      * color components.  All of the bits in each mask must be contiguous
  67.      * and fit in the specified number of least significant bits of the
  68.      * integer.
  69.      */
  70.     public DirectColorModel(int bits,
  71.                 int rmask, int gmask, int bmask, int amask) {
  72.     super(bits);
  73.     red_mask = rmask;
  74.     green_mask = gmask;
  75.     blue_mask = bmask;
  76.     alpha_mask = amask;
  77.     CalculateOffsets();
  78.     }
  79.  
  80.     /**
  81.      * Return the mask indicating which bits in a pixel contain the red
  82.      * color component.
  83.      */
  84.     public int getRedMask() {
  85.     return red_mask;
  86.     }
  87.  
  88.     /**
  89.      * Return the mask indicating which bits in a pixel contain the green
  90.      * color component.
  91.      */
  92.     public int getGreenMask() {
  93.     return green_mask;
  94.     }
  95.  
  96.     /**
  97.      * Return the mask indicating which bits in a pixel contain the blue
  98.      * color component.
  99.      */
  100.     public int getBlueMask() {
  101.     return blue_mask;
  102.     }
  103.  
  104.     /**
  105.      * Return the mask indicating which bits in a pixel contain the alpha
  106.      * transparency component.
  107.      */
  108.     public int getAlphaMask() {
  109.     return alpha_mask;
  110.     }
  111.  
  112.     private int accum_mask = 0;
  113.  
  114.     /*
  115.      * A utility function to decompose a single mask and verify that it
  116.      * fits in the specified pixel size, and that it does not overlap any
  117.      * other color component.  The values necessary to decompose and
  118.      * manipulate pixels are calculated as a side effect.
  119.      */
  120.     private void DecomposeMask(int mask, String componentName, int values[]) {
  121.     if ((mask & accum_mask) != 0) {
  122.         throw new IllegalArgumentException(componentName + " mask bits not unique");
  123.     }
  124.     int off = 0;
  125.     int count = 0;
  126.     if (mask != 0) {
  127.         while ((mask & 1) == 0) {
  128.         mask >>>= 1;
  129.         off++;
  130.         }
  131.         while ((mask & 1) == 1) {
  132.         mask >>>= 1;
  133.         count++;
  134.         }
  135.     }
  136.     if (mask != 0) {
  137.         throw new IllegalArgumentException(componentName + " mask bits not contiguous");
  138.     }
  139.     if (off + count > pixel_bits) {
  140.         throw new IllegalArgumentException(componentName + " mask overflows pixel");
  141.     }
  142.     values[0] = off;
  143.     values[1] = count;
  144.     }
  145.  
  146.     /*
  147.      * A utility function to verify all of the masks and to store
  148.      * the auxilliary values needed to manipulate the pixels.
  149.      */
  150.     private void CalculateOffsets() {
  151.     int values[] = new int[2];
  152.     DecomposeMask(red_mask, "red", values);
  153.     red_offset = values[0];
  154.     red_bits = values[1];
  155.     DecomposeMask(green_mask, "green", values);
  156.     green_offset = values[0];
  157.     green_bits = values[1];
  158.     DecomposeMask(blue_mask, "blue", values);
  159.     blue_offset = values[0];
  160.     blue_bits = values[1];
  161.     DecomposeMask(alpha_mask, "alpha", values);
  162.     alpha_offset = values[0];
  163.     alpha_bits = values[1];
  164.     }
  165.  
  166.     /**
  167.      * Return the red color compoment for the specified pixel in the
  168.      * range 0-255.
  169.      */
  170.     public int getRed(int pixel) {
  171.     if (red_mask == 0) return 0;
  172.     int r = ((pixel & red_mask) >>> red_offset);
  173.     return (red_bits == 8) ? r : (r * 255 / ((1 << red_bits) - 1));
  174.     }
  175.  
  176.     /**
  177.      * Return the green color compoment for the specified pixel in the
  178.      * range 0-255.
  179.      */
  180.     public int getGreen(int pixel) {
  181.     if (green_mask == 0) return 0;
  182.     int g = ((pixel & green_mask) >>> green_offset);
  183.     return (green_bits == 8) ? g : (g * 255 / ((1 << green_bits) - 1));
  184.     }
  185.  
  186.     /**
  187.      * Return the blue color compoment for the specified pixel in the
  188.      * range 0-255.
  189.      */
  190.     public int getBlue(int pixel) {
  191.     if (blue_mask == 0) return 0;
  192.     int b = ((pixel & blue_mask) >>> blue_offset);
  193.     return (blue_bits == 8) ? b : (b * 255 / ((1 << blue_bits) - 1));
  194.     }
  195.  
  196.     /**
  197.      * Return the alpha transparency value for the specified pixel in the
  198.      * range 0-255.
  199.      */
  200.     public int getAlpha(int pixel) {
  201.     if (alpha_mask == 0) return 255;
  202.     int a = ((pixel & alpha_mask) >>> alpha_offset);
  203.     return (alpha_bits == 8) ? a : (a * 255 / ((1 << alpha_bits) - 1));
  204.     }
  205. }
  206.